<?php

/**
 * 智慧安监控制器
 * User: sunyanyou
 * Date: 2020-05-08
 */
namespace Admin\Controller;
class WisdomController extends CommonController{
	/**
     * 企业培训列表
     */
    public function train(){
        $count = M('train')->count();
        $page = getpage($count,15);
        $show = $page->show();
    	$list = M('train')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        $company = M('company')->select();
    	$this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('company',$company);
    	$this->display('train_list');
    }
    /**
     * 安全投入列表
     */
    public function safety_pay(){
        $count = M('safety_pay')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('safety_pay')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        $user = M('user')->select();
        $this->assign('user',$user);
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display();
    }
    /**
     * 新员工三级安全教育
     */
    public function safety_education(){
        $count = M('safety_education')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('safety_education')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        $company = M('company')->select();
        $this->assign('company',$company);
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display();
    }
    /**
     * 新员工三级安全教育详情
     */
    public function safety_education_detail(){
        if(IS_POST){
            $id = I('post.id');
            $data['company_id'] = I('company_id');
            $data['name'] = I('name');
            $data['sex'] = I('sex');
            $data['position'] = I('position');
            $data['factory_time'] = I('factory_time');
            $data['workshop'] = I('workshop');
            $data['card_id'] = I('card_id');
            $data['phone'] = I('phone');
            $data['is_special'] = I('is_special');
            $data['kind'] = I('kind');
            $data['license_date'] = I('license_date');
            $data['review_date'] = I('review_date');
            $data['company_level'] = I('company_level');
            $data['workshop_level'] = I('workshop_level');
            $data['team_level'] = I('team_level');
            $train_img = json_encode(I('train_img'));
            $data['train_img'] = $train_img;
            $data['create_time'] = date("Y-m-d H:i:s",time());
            /*if(!empty($data)){
                $result = M('safety_education')->where(['id'=>$id])->save($data);
                $this->success('修改成功',U('Wisdom/safety_education'));
            }*/
        }
        $id = I('id');
        $info = M('safety_education')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$info['train_img'].$behind;
        $images = json_decode($json,true);
        /*echo $_SERVER['HTTP_HOST'];
        print_r($images);exit;*/
        //$_SERVER['HTTP_HOST'];
        $where['id']=$info['company_id'];
        
        //$company = M('company')->where('status=1')->select();
        $company = M('company')->where($where)->find();
        /*echo  M('company')->getLastSql();
        print_r($company);exit;*/
        $this->assign('info',$info);
        $this->assign('images','http://'.$_SERVER['HTTP_HOST'].'/'.$images[0]);
        $this->assign('company',$company);
        $this->display();
    }
    /**
     * 添加企业培训
     */
    public function add_train(){
        if(IS_POST){
            $data['company_id'] = I('company_id');
            $data['train_name'] = I('train_name');
            $data['train_content'] = I('train_content');
            $data['train_time'] = I('train_time');
            $image = json_encode(I('train_photo'));
            $data['train_photo'] = $image;
            $data['is_plan'] = I('is_plan');
            $plan_img = json_encode(I('plan_img'));
            $data['plan_img'] = $plan_img;
            $data['create_time'] = time();
            if(!empty($data)){
                $result = M('train')->add($data);
                $this->success('添加成功',U('Wisdom/train'));
            }
        }
        $company = M('company')->where('status=1')->select();
        $this->assign('company',$company);
        $this->display();
    }
    /**
     * 添加企业安全投入
     */
    public function add_safety_pay(){
        if(IS_POST){
            $data['user_id'] = I('user_id');
            $data['company_id'] = I('company_id');
            $company = M('company')->where(['id'=>$data['company_id']])->find();
            $data['company_name'] = $company['name'];
            $data['safety_input_year'] = I('safety_input_year');
            $data['last_year_income'] = I('last_year_income');
            $data['safety_standard'] = I('safety_standard');
            $data['should_take_income'] = I('should_take_income');
            $data['fact_take_income'] = I('fact_take_income');
            $data['totale_safety_pay'] = I('totale_safety_pay');
            $image = json_encode(I('safety_pay_photo'));
            $data['safety_pay_photo'] = $image;
            $data['create_time'] = time();
            if(!empty($data)){
                $result = M('safety_pay')->add($data);
                $this->success('添加成功',U('Wisdom/safety_pay'));
            }
        }
        $user = M('user')->where('level_id=5')->select();
        $company = M('company')->select();
        $this->assign('user',$user);
        $this->assign('company',$company);
        $this->display();
    }
    /**
     * 添加企业新员工三级安全教育
     */
    public function add_safety_education(){
        if(IS_POST){
            $data['company_id'] = I('company_id');
            $data['name'] = I('name');
            $data['sex'] = I('sex');
            $data['position'] = I('position');
            $data['factory_time'] = I('factory_time');
            $data['workshop'] = I('workshop');
            $data['card_id'] = I('card_id');
            $data['phone'] = I('phone');
            $data['is_special'] = I('is_special');
            $data['kind'] = I('kind');
            $data['license_date'] = I('license_date');
            $data['review_date'] = I('review_date');
            $data['company_level'] = I('company_level');
            $data['workshop_level'] = I('workshop_level');
            $data['team_level'] = I('team_level');
            $image = json_encode(I('train_img'));
            $data['train_img'] = $image;
            $data['create_time'] = date("Y-m-d H:i:s",time());
            if(!empty($data)){
                $result = M('safety_education')->add($data);
                $this->success('添加成功',U('Wisdom/safety_education'));
            }
        }
        $company = M('company')->where('status=1')->select();
        $this->assign('company',$company);
        $this->display();
    }
    /**
     * 修改企业新员工三级安全教育
     */
    public function edit_safety_education(){
        if(IS_POST){
            $id = I('post.id');
            $data['company_id'] = I('company_id');
            $data['name'] = I('name');
            $data['sex'] = I('sex');
            $data['position'] = I('position');
            $data['factory_time'] = I('factory_time');
            $data['workshop'] = I('workshop');
            $data['card_id'] = I('card_id');
            $data['phone'] = I('phone');
            $data['is_special'] = I('is_special');
            $data['kind'] = I('kind');
            $data['license_date'] = I('license_date');
            $data['review_date'] = I('review_date');
            $data['company_level'] = I('company_level');
            $data['workshop_level'] = I('workshop_level');
            $data['team_level'] = I('team_level');
            $train_img = json_encode(I('train_img'));
            $data['train_img'] = $train_img;
            $data['create_time'] = date("Y-m-d H:i:s",time());
            if(!empty($data)){
                $result = M('safety_education')->where(['id'=>$id])->save($data);
                $this->success('修改成功',U('Wisdom/safety_education'));
            }
        }
        $id = I('id');
        $info = M('safety_education')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$info['train_img'].$behind;
        $images = json_decode($json,true);
        $company = M('company')->where('status=1')->select();
        $this->assign('info',$info);
        $this->assign('images',$images[0]);
        $this->assign('company',$company);
        $this->display();
    }
    /**
     * 删除企业新员工三级安全教育
     */
    public function del_safety_education(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('safety_education')->where(['id'=>$id])->delete();
            $this->success('删除成功',U('Wisdom/safety_education'));
        }
    }
    /**
     * 修改企业培训
     */
    public function edit_train(){
        if(IS_POST){
            $id = I('post.id');
            $data['company_id'] = I('company_id');
            $data['train_name'] = I('train_name');
            $data['train_content'] = I('train_content');
            $data['train_time'] = I('train_time');
            $image = json_encode(I('train_photo'));
            $data['train_photo'] = $image;
            $data['is_plan'] = I('is_plan');
            $plan_img = json_encode(I('plan_img'));
            $data['plan_img'] = $plan_img;
            $data['create_time'] = time();
            if(!empty($data)){
                $result = M('train')->where(['id'=>$id])->save($data);
                $this->success('修改成功',U('Wisdom/train'));
            }
        }
        $id = I('id');
        $info = M('train')->where(['id'=>$id])->find();
        $company = M('company')->where('status=1')->select();
        $front = "[";
        $behind = "]";
        $json = $front.$info['train_photo'].$behind;
        $images = json_decode($json,true);
        $vfront = '[';
        $vbehind = ']';
        $vjson = $vfront.$info['plan_img'].$vbehind;
        $plan = json_decode($vjson,true);
        $this->assign('info',$info);
        $this->assign('company',$company);
        $this->assign('images',$images[0]);
        $this->assign('plan',$plan[0]);
        $this->display();
    }
            
    /**
     * 修改企业安全投入
     */
    public function edit_safety_pay(){
        if(IS_POST){
            $id = I('post.id');
            $data['user_id'] = I('user_id');
            $data['company_id'] = I('company_id');
            $company = M('company')->where(['id'=>$data['company_id']])->find();
            $data['company_name'] = $company['name'];
            $data['safety_input_year'] = I('safety_input_year');
            $data['last_year_income'] = I('last_year_income');
            $data['safety_standard'] = I('safety_standard');
            $data['should_take_income'] = I('should_take_income');
            $data['fact_take_income'] = I('fact_take_income');
            $data['totale_safety_pay'] = I('totale_safety_pay');
            $image = json_encode(I('safety_pay_photo'));
            $data['safety_pay_photo'] = $image;
            $data['create_time'] = time();
            
            if(!empty($data)){
                $result = M('safety_pay')->where(['id'=>$id])->save($data);
                $this->success('修改成功',U('Wisdom/safety_pay'));
            }
        }
        $id = I('id');
        $info = M('safety_pay')->where(['id'=>$id])->find();
        $user = M('user')->where('level_id=5')->select();
        $company = M('company')->where('status=1')->select();
        $front = '[';
        $behind = ']';
        $json = $front.$info['safety_pay_photo'].$behind;
        $images = json_decode($json,true);
        
        $this->assign('info',$info);
        $this->assign('user',$user);
        $this->assign('company',$company);
        $this->assign('images',$images[0]);
        $this->display();
    }
    /**
     * 查看企业培训图片
     */
    public function train_photos(){
        $id = I('id');
        $photos = M('train')->field('train_photo')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['train_photo'].$behind;
        $images = json_decode($json,true);
        // $images = json_decode($photos['report_photo'],true);
        // print_r($images);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 查看企业年度培训计划图片
     */
    public function plan_photos(){
        $id = I('id');
        $photos = M('train')->field('plan_img')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['plan_img'].$behind;
        $images = json_decode($json,true);
        // $images = json_decode($photos['report_photo'],true);
        // print_r($images);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 查看企业安全投入图片
     */
    public function safety_pay_photos(){
        $id = I('id');
        $photos = M('safety_pay')->field('safety_pay_photo')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['safety_pay_photo'].$behind;
        $images = json_decode($json,true);
        
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 查看企业新员工三级安全教育培训图片
     */
    public function safety_education_photos(){
        $id = I('id');
        $photos = M('safety_education')->field('train_img')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['train_img'].$behind;
        $images = json_decode($json,true);
        
        $this->assign('images',$images);
        $this->display('drill_photos');
    }
    /**
     * 删除企业培训
     */
    public function del_train(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('train')->where(['id'=>$id])->delete();
            $this->success('删除成功',U('Wisdom/train'));
        }
    }
    /**
     * 删除企业安全投入
     */
    public function del_safety_pay(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('safety_pay')->where(['id'=>$id])->delete();
            $this->success('删除成功',U('Wisdom/safety_pay'));
        }
    }
    /**
     * 查看安全演练图片
     */
    public function drill_photos(){
        $id = I('id');
        $photos = M('emergency_drill')->field('drill_photo')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['drill_photo'].$behind;
        $images = json_decode($json,true);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 查看应急演练计划图片
     */
    public function drill_plan_photos(){
        $id = I('id');
        $photos = M('emergency_drill')->field('plan_photo')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['plan_photo'].$behind;
        $images = json_decode($json,true);
        $this->assign('images',$images);
        $this->display('drill_photos');
    }
    /**
     * 企业应急演练列表
     */
    public function safetyDrill(){
        $count = M('emergency_drill')->count();
        $page = getpage($count,15);
        $show = $page->show();
    	$list = M('emergency_drill')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        $company = M('company')->select();
    	$this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$page);
        $this->assign('company',$company);
    	$this->display('drill_list');
    }
    /**
     * 添加安全演练
     */
    public function add_drill(){
        if(IS_POST){
            $data['is_plan'] = I('is_plan');
            $plan_photo = json_encode(I('plan_photo'));
            $data['plan_photo'] = $plan_photo;
            $data['company_id'] = I('company_id');
            $data['drill_time'] = I('drill_time');
            $data['drill_address'] = I('drill_address');
            $data['drill_item'] = I('drill_item');
            $data['is_program'] = I('is_program');
            $data['drill_process'] = I('drill_process');
            $data['drill_evaluate'] = I('drill_evaluate');
            $image = json_encode(I('drill_photo'));
            $data['drill_photo'] = $image;
            $data['create_time'] = time();
            if(!empty($data)){
                $result = M('emergency_drill')->add($data);
                $this->success('添加成功',U('Wisdom/safetyDrill'));
            }
        }
        $company = M('company')->where('status=1')->select();
        $this->assign('company',$company);
        $this->display();
    }
    /**
     * 修改安全演练
     */
    public function edit_drill(){
        if(IS_POST){
            $id = I('post.id');
            $data['is_plan'] = I('is_plan');
            $plan_photo = json_encode(I('plan_photo'));
            $data['plan_photo'] = $plan_photo;
            $data['company_id'] = I('company_id');
            $data['drill_time'] = I('drill_time');
            $data['drill_address'] = I('drill_address');
            $data['drill_item'] = I('drill_item');
            $data['is_program'] = I('is_program');
            $data['drill_process'] = I('drill_process');
            $data['drill_evaluate'] = I('drill_evaluate');
            $image = json_encode(I('drill_photo'));
            $data['drill_photo'] = $image;
            $data['create_time'] = time();
            if(!empty($data)){
                $result = M('emergency_drill')->where(['id'=>$id])->save($data);
                $this->success('修改成功',U('Wisdom/safetyDrill'));
            }
        }
        $id = I('id');
        $info = M('emergency_drill')->where(['id'=>$id])->find();
        $company = M('company')->where('status=1')->select();
        $front = "[";
        $behind = "]";
        $json = $front.$info['drill_photo'].$behind;
        $images = json_decode($json,true);
        $vfront = "[";
        $vbehind = "]";
        $vjson = $vfront.$info['plan_photo'].$vbehind;
        $plan_photo = json_decode($vjson,true);
        $this->assign('info',$info);
        $this->assign('company',$company);
        $this->assign('images',$images[0]);
        $this->assign('plan_photo',$plan_photo[0]);
        $this->display();
    }
    /**
     * 删除安全演练
     */
    public function del_drill(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('safety_drill')->where(['id'=>$id])->delete();
            $this->success('删除成功',U('Wisdom/safetyDrill'));
        }
    }
    /**
     * 风险排查列表
     */
    public function troubleshooting(){
    	$list = M('danger')->select();
        $company = M('company')->select();
    	$this->assign('list',$list);
        $this->assign('company',$company);
    	$this->display('trouble_list');
    }
    //
    public function kongzhi(){
        $drill = M('safety_drill')->select();
        $list = [];
        foreach ($drill as $key => $value) {
            $transform = json_decode($value['drill_photo'],true);
            print_r($transform[0]);
            array_push($list,$value['id'],$value['company_id'],$value['drill_name'],$value['drill_time']);

        }
        print_r($list);
        // $result = array_chunk($list, 2);
        // print_r($result);
        $this->assign('list',$list);
        //print_r($list);
        // $aa = '["Uploads\/pc\/20200509\/5eb6723e81c2b.jpg","Uploads\/pc\/20200509\/5eb6723e820ab.jpg","Uploads\/pc\/20200509\/5eb6723e8242a.jpg","Uploads\/pc\/20200509\/5eb6723e827c9.jpg"]';
        // print_r(json_decode($aa,true));
    }
    /**
     * 审批项目
     */
    public function report_item(){
        $count = M('approval_type')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('approval_type')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display('item_list');
    }
    /**
     * 添加审批项目
     */
    public function add_report_item(){
        if (IS_POST) {
            $data['name'] = I('post.name');
            $res = M('approval_type')->add($data);       
            if (empty($res)) {
                $this->error('服务器繁忙，请稍后再试！');
            }
            $this->success('添加成功', U('Wisdom/report_item'));
        }
        $this->display('add_report_item');
    }
    /**
     * 编辑审批项目
     */
    public function edit_report_item(){
        if (IS_POST) {
            $id = I('id');
            $data['name'] = I('post.name');
            $res = M('approval_type')->where(['id' => $id])->save($data);       
            $this->success('保存成功', U('Wisdom/report_item'));
        }
        $id = I('id');
        $info = M('approval_type')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display('edit_report_item');
    }
    /**
     * 删除审批项目
     */
    public function del_report_item(){
        if (IS_AJAX) {
            $id = I('id');
            M('approval_type')->where(['id' => $id])->delete();
            $this->success('删除成功', U('Wisdom/report_item'));
        }
    }
    /**
     * 安全员审批项目列表
     */
    public function report(){
        $user_id = session('user_id');
        $user_company = M('user')->where(['user_id'=>$user_id])->find();
        if(empty($user_id)){
            $count = M('report')->order('id desc')->count();
        }else{
            $count = M('report')->order('id desc')->where(array("company_id"=>array("in", $user_company['company_id'])))->count();
        }
        //$count = M('report')->count();
        $page = getpage($count,15);
        $show = $page->show();
        if(empty($user_id)){
            $list = M('report')->limit($page->firstRow.','.$page->listRows)->select();
        }else{
            $list = M('report')->limit($page->firstRow.','.$page->listRows)->where(array("company_id"=>array("in", $user_company['company_id'])))->select();
        }
        $type = M('approval_type')->select();
        $data['status'] = '1';
        $data['level'] = '5';
        $user = M('user')->where($data)->select();
        $company = M('company')->select();
        $level = $user_company['level_id'];
        $this->assign('level',$level);
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('type',$type);
        $this->assign('user',$user);
        $this->assign('company',$company);
        $this->display('report_list');
    }
    /**
     * 安全员上报审批项目图片
     */
    public function report_photos(){
        $id = I('id');
        $photos = M('report')->field('report_photo')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['report_photo'].$behind;
        $images = json_decode($json,true);
        // $images = json_decode($photos['report_photo'],true);
        // print_r($images);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 安全员上报审批项目视频
     */
    public function report_video(){
        $id = I('id');
        $video = M('report')->field('report_video')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$video['report_video'].$behind;
        $images = json_decode($json,true);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 安全员日志项目列表
     */
    public function logs(){
        $count = M('logs')->count();
        $page = getpage($count,15);
        $show = $page->show();
        $list = M('logs')->limit($page->firstRow.','.$page->listRows)->select();
        $data['status'] = '1';
        $data['level'] = '5';
        $user = M('user')->where($data)->select();
        $company = M('company')->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('user',$user);
        $this->assign('company',$company);
        $this->display('log_list');
    }
    /**
     * 安全员日志图片
     */
    public function log_photos(){
        $id = I('id');
        $photos = M('logs')->field('log_photo')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$photos['log_photo'].$behind;
        $images = json_decode($json,true);
        // $images = json_decode($photos['report_photo'],true);
        // print_r($images);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 安全员上报审批项目视频
     */
    public function log_video(){
        $id = I('id');
        $video = M('logs')->field('log_video')->where(['id'=>$id])->find();
        $front = "[";
        $behind = "]";
        $json = $front.$video['log_video'].$behind;
        $images = json_decode($json,true);
        $this->assign('images',$images);
        $this->display();
    }
    /**
     * 管理员审批安全员上报项目
     */
    public function ajaxUpdateStatus(){
        $admin_id = session('admin_id');
        if(IS_AJAX){
            $id = I('id');
            $type = '2';
            $result = M('report')->where(['id'=>$id])->setField('status',1);
            $report = M('report')->where(['id'=>$id])->find();
            $list = M('user')->where(['user_id'=>$report['uid']])->find();
            $handle = M('handle_list')->where(['item_id'=>$report['id']])->where(['handle_type'=>$type])->find();  
            $mobile = $list['phone']; 
            $this->http_send_post($mobile,'【临沭安监】你的上报项目已经审批通过。');
            $user_id = $list['user_id'];
            $j_push_id = M('User')->where(['user_id' => $user_id])->getField('j_push_id');
            if (!empty($j_push_id)) {
                jPushs('你的上报项目已经审批通过！', $j_push_id);
            }    
            if(!empty($handle)){
                $data['item_id'] = $report['id'];
                $data['handle_type'] = '2';
                $data['push_user_id'] = $report['uid'];
                $data['status'] = '1';
                $data['deal_user_id'] = $admin_id;
                $data['handle_time'] = time();
                $res = M('handle_list')->where(['id'=>$handle['id']])->save($data);
                $info = array('info'=>'审批已通过', 'status'=>1, 'url'=>'');
                $this->ajaxReturn($info);
            }else{
                $info = array('info'=>'失败', 'status'=>0, 'url'=>'');
                $this->ajaxReturn($info);
            }
            
            

        }
    }
    /**
     * 执法人员审批安全员上报项目
     */
    public function ajaxLawUpdateStatus(){
        $user_id = session('user_id');
        if(IS_AJAX){
            $id = I('id');
            $type = '2';
            $result = M('report')->where(['id'=>$id])->setField('status',1);
            $report = M('report')->where(['id'=>$id])->find();
            $list = M('user')->where(['user_id'=>$report['uid']])->find();
            $handle = M('handle_list')->where(['item_id'=>$report['id']])->where(['handle_type'=>$type])->find(); 
            $mobile = $list['phone']; 
            $this->http_send_post($mobile,'【临沭安监】你的上报项目已经审批通过。');
            $user_id = $list['user_id'];
            $j_push_id = M('User')->where(['user_id' => $user_id])->getField('j_push_id');
            if (!empty($j_push_id)) {
                jPushs('你的上报项目已经审批通过！', 0, $j_push_id, $user_id);
            }         
            if(!empty($handle)){
                $data['item_id'] = $report['id'];
                $data['handle_type'] = '2';
                $data['push_user_id'] = $report['uid'];
                $data['status'] = '1';
                $data['deal_user_id'] = $user_id;
                $data['handle_time'] = time();
                $res = M('handle_list')->where(['id'=>$handle['id']])->save($data);
                $info = array('info'=>'审批已通过', 'status'=>1, 'url'=>'');
                $this->ajaxReturn($info);
            }else{
                $info = array('info'=>'失败', 'status'=>0, 'url'=>'');
                $this->ajaxReturn($info);
            }
            
            

        }
    }
    /**
    短信发送页面
    */
    public function send_msg(){
        $usercompany = M('user')->where('status=1')->where('level_id=5')->select();
        $userlaw = M('user')->where('status=1')->where('level_id=1')->select();
        $this->assign('usercompany',$usercompany);
        $this->assign('userlaw',$userlaw);
        $this->display();
    }

    //开始发送短信并存入数据库
    public function add_send_msg(){
        if(IS_POST){
            $user_company_id = I('user_company_id');
            $user_law_id = I('user_law_id');
            $send_phone = I('send_phone');
            $send_content = I('send_content');

            if(!empty($user_company_id)){
                $user = M('user')->where('level_id=5')->where(['user_id'=>$user_company_id])->find();
                $mobile = $user['phone'];
            }
            if(!empty($user_law_id)){
                $user = M('user')->where('level_id=1')->where(['user_id'=>$user_law_id])->find();
                $mobile = $user['phone'];
            }
            if(!empty($send_phone)){
                $mobile = $send_phone;
            }
            $data['send_person'] = I('user_company_id');
            $data['send_person'] = I('user_law_id');
            $data['send_phone'] = $mobile;
            $data['send_content'] = I('send_content');
            $data['send_time'] = time();
            $data['send_status'] = '1';
            $result = M('send_history')->add($data);
            $msg = '【临沭安监】'.$send_content;
            $res = $this->http_send_post($mobile,$msg);
            $this->success('发送成功','Wisdom/send_msg');
        }
    }
    /**
     * 短信发送记录
     */
    public function send_history(){
        $count = M('send_history')->count();
        $page = getpage($count,20);
        $show = $page->show();
        $list = M('send_history')->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
        $user = M('user')->where('status=1')->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->assign('user',$user);
        $this->display();
    }
    /**
     * 删除短信发送记录
     */
    public function del_send_history(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('send_history')->where(['id'=>$id])->delete();
            if($result){
                $this->success('删除成功','Wisdom/send_history');
            }
        }
    }
    /**
     * 发送短信
     * @param $phone string 手机号
     * @param $msg string 内容
     * @return string
     */
    public function http_send_post($mobile, $msg)
    {
        $url = 'http://115.28.96.167:8081/api/sms/send';   //发送接口
        $post_data['userid']       = '351481';    //用户编号
        $post_data['apikey']       =  'ef9ad3d6bdfe4732812f191ca9f107e0';        //apikey  
        $t = explode(" ", microtime());
        $ts = round(round($t[1].substr($t[0],2,3)));
        $post_data['ts']      = $ts ;   //时间戳，精确到毫秒
        $post_data['sign']  = md5($post_data['userid'].$post_data['ts'].$post_data['apikey']);      //签名
        $post_data['mobile']       = $mobile;                //手机号
        $post_data['msgcontent']      = $msg;                     //发送内容

        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);
        $result = $this->request_post($url, $post_data);
    }

    function request_post($url = '', $param = '') {
    if (empty($url) || empty($param)) {
        return false;
    }
    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    return $data;
}
}
